<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<?php
require_once "../php/objetos/Email.php";
require_once "../php/objetos/Usuario.php";
require_once "../php/GerenciarEmails.php";
session_start();
if(!array_key_exists("userId", $_SESSION) && $_SESSION["userId"] == NULL){
    header("Location: ../index.php");
}
$userId = $_SESSION["userId"];
$emails = listarEmailsEnviadosUsuario($userId);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="../js/jquery-1.7.js" type="text/javascript"></script>
        <script type="text/javascript">
            function verificarStatusEmail(){
                var tabela = document.getElementById("tableEmails");
                var linhas = tabela.getElementsByTagName("tr");
                    for (var i = 0; i < linhas.length; i++) { 
                        var inputs = linhas[i].getElementsByTagName("input");
                        for (var j = 0; j < inputs.length; j++) {
                            if(inputs[j].name == "statusEmail" && inputs[j].value != 1){
                                linhas[i].style.fontWeight = "bold"; 
                            }
                        }                       
                }
            }
            
            function checkAllEmails(retorno) {
                var formulario = document.form1;
                if(retorno==true){
                    for(i=0;i<formulario.length;i++){
                        if(formulario.elements[i].type=="checkbox" && formulario.elements[i].name!="checkAllEmail"){
                          if(formulario.elements[i].checked==false){
                            formulario.elements[i].checked=true;
                          }
                        }
                    }
                } else {
                    for(i=0;i<formulario.length;i++){
                        if(formulario.elements[i].type=="checkbox" && formulario.elements[i].name!="checkAllEmail"){
                          if(formulario.elements[i].checked==true){
                            formulario.elements[i].checked=false;
                          }
                        }
                    }
                }
            }
            
            function deletarEmails(excluirLink){
                var formulario = document.form1;
                var stringIdEmails = "";
                
                for(i=0;i<formulario.length;i++){
                    if(formulario.elements[i].type=="checkbox" && formulario.elements[i].name!="checkAllEmail"){
                      if(formulario.elements[i].checked==true){
                          countIdEmail = i + 2;
                          var idEmail = formulario.elements[countIdEmail].value;
                          stringIdEmails += idEmail + ";";
                      }
                    }
                }
                
                excluirLink.href = "../php/ExcluirEmails.php?idEmails="+stringIdEmails+"&enviados=1";
            }

            function mouseSobreLinha(linha){
                linha.style.cursor = "pointer";
                linha.style.background = "#1773BC";
            }
            
            function mouseForaDaLinha(linha){
                linha.style.cursor = "default";
                linha.style.background = "transparent";
            }
            
            function verEmail(idEmail){
                location.href = "../php/EmailVer.php?idEmail="+idEmail+"&enviado=1";
            }
            
        </script>
        <link rel="stylesheet" type="text/css" href="../css/style.css"/>
        <title>WEBEMAIL</title>
    </head>
    <body onload="verificarStatusEmail();">
        <div id="header">
            <img src="../css/imagens/header.jpg" />
            <div id="logout">
                Bem Vindo, 
                <?php 
                    echo $userId->getNome();
                ?>
                
                <a href="../php/Logout.php"><img src="../css/imagens/exit.png" height="20" width="20" /></a>
            </div>
        </div>
        <div id="menu">
            <h4>Menu</h4>
            <ul>
                <li><a href="principal.php">Caixa de Entrada</a></li>
                <li><a href="lixeira.php">Lixo</a></li>
                <li><a href="rascunho.php">Rascunho</a></li>
                <li><a href="itensEnviados.php"><img alt="seta" src="../css/imagens/btn_seta_2.png" class="iconSeta"/>Itens Enviados</a></li>
            </ul>
        </div>
        <div id="content">
            
            <div id="menuEmail">
                <ul>
                    <li><a id="excluir" alt="" href="" onclick="deletarEmails(this);">Excluir</a></li>
                </ul>
            </div>
            <form id="form1" name="form1">
                <h4>Itens Enviados</h4>
                <div id="gridMsg">
                        <?php
                        if ($emails != null) {?>
                            <table id="tableEmails" cellpadding="2px" cellspacing="0">
                                <thead>
                                    <th><input id="checkAllEmail" name="checkAllEmail" type="checkbox" onclick="checkAllEmails(this.checked);"/></th>
                                    <th>Destinatário</th>
                                    <th>Assunto</th>
                                    <th>Data de Envio</th>
                                    <th></th>
                                    <th></th>
                                </thead>
                                <tbody>
                            <?php foreach ($emails as $email) {?>
                                    <tr id="linhaEmail" onmouseover="mouseSobreLinha(this);" onmouseout="mouseForaDaLinha(this)" onclick="verEmail('<?php echo $email->getId();?>');">
                                        <td><input id="checkEmail" type="checkbox" /></td>
                                        <td><?php echo $email->getDestinatario(); ?></td>
                                        <td><?php echo $email->getAssunto();?> </td>
                                        <td><?php echo $email->getDataEnviado();?> </td>
                                        <td><input id="statusEmail" name="statusEmail" type="hidden" value="<?php echo $email->getStatus();?>"/></td>
                                        <td><input id="idEmail" name="idEmail" type="hidden" value="<?php echo $email->getId();?>"/></td>
                                    </tr>
                            <?php } ?>
                                </tbody>
                            </table>
                    <?php } else {
                        echo "Não Há Emails\n";
                    }?>
                </div>
            </form>
        </div>
        <div id="footer">
            @WebMail Todos Direitos Reservados
        </div>
    </body>
</html>
